import { Request, toSuccess } from "@sker/core";
import axios from 'axios';
import { DependencyContainer } from "tsyringe";
export default async (injector: DependencyContainer) => {
    const req = injector.resolve(Request)
    const clientIp = getClientIp(req)
    const url = `https://searchplugin.csdn.net/api/v1/ip/get?ip=${clientIp}`
    const result = await axios.get(url).then(res => res.data)
    const { ip, address } = result.data || {};
    return toSuccess({ ip, address })
}

function getClientIp(req: any) {
    return req.headers['x-forwarded-for'] ||
        req.ip ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress ||
        '';
}

